﻿#include "mandb.h"
#include <QDateTime>
Q_GLOBAL_STATIC(ManDB, ins)
ManDB::ManDB(QObject* parent)
    : QObject(parent)
{}
ManDB* ManDB::instance()
{
    return ins();
}
void ManDB::init()
{
    m_daoLoginInfo.connect();
    m_daoLoginInfo.createTable();
    m_loginfoList = m_daoLoginInfo.select();
}
void ManDB::saveLoginInfo(const QString& name, const QString& id, const QString& key,
                          const QString& remark)
{
    LoginInfo info;
    info.name       = (name == "" ? id : name);
    info.secret_id  = id.trimmed();
    info.secret_key = key.trimmed();
    info.remark     = remark.trimmed();
    info.timestamp  = QDateTime::currentDateTimeUtc().toTime_t();
    if (m_daoLoginInfo.exists(info.secret_id)) {
        m_daoLoginInfo.update(info);
        m_loginfoList[indexOfLoginInfo(info.secret_id)] = info;
    }
    else {
        m_daoLoginInfo.insert(info);
        m_loginfoList.append(info);
    }
}
void ManDB::removeLoginInfo(const QString& id)
{
    if (m_daoLoginInfo.exists(id)) {
        m_daoLoginInfo.remove(id);
        m_loginfoList.removeAt(indexOfLoginInfo(id));
    }
}
int ManDB::indexOfLoginInfo(const QString& secretId)
{
    for (int i = 0; i < m_loginfoList.size(); ++i) {
        if (m_loginfoList[i].secret_id == secretId) { return i; }
    }
    throw QString::fromLocal8Bit("获取登录信息索引失败 %1").arg(secretId);
}
QStringList ManDB::loginNameList()
{
    QStringList words;
    for (int i = 0; i < m_loginfoList.size(); ++i) { words.append(m_loginfoList[i].name); }
    return words;
}
LoginInfo ManDB::loginInfoByName(const QString& name)
{
    for (int i = 0; i < m_loginfoList.size(); ++i) {
        if (m_loginfoList[i].name == name) { return m_loginfoList[i]; }
    }
    throw QString::fromLocal8Bit("通过名称查找登录信息失败 %1").arg(name);
}
ManDB::~ManDB() {}
